      1  /**

      2     The sort method of this class sorts an array, using the selection

      3     sort algorithm.

      4  */

      5  public class SelectionSorter

      6  {

      7     /**

      8        Sorts an array, using selection sort.

      9        @param a the array to sort

     10     */

     11     public static void sort(int[] a)

     12     {

     13        for (int i = 0; i < a.length - 1; i++)

     14        {

     15           int minPos = minimumPosition(a, i);

     16           ArrayUtil.swap(a, minPos, i);

     17        }

     18     }

     19

     20     /**

     21        Finds the smallest element in a tail range of the array.

     22        @param a the array to sort

     23        @param from the first position in a to compare

     24        @return the position of the smallest element in the

     25        range a[from] . . . a[a.length - 1]

     26     */

     27     private static int minimumPosition(int[] a, int from)

     28     {

     29        int minPos = from;

     30        for (int i = from + 1; i < a.length; i++)

     31        {

     32           if (a[i] < a[minPos]) { minPos = i; }

     33        }

     34        return minPos;

     35     }

     36  }